import 'package:film/screen/home_screen.dart';
import 'package:film/screen/mine_screen.dart';
import 'package:film/screen/share_screen.dart';
import 'package:flutter/material.dart';

class TabScreen extends StatefulWidget {
  const TabScreen({super.key});

  @override
  State<TabScreen> createState() => _TabScreenState();
}

class _TabScreenState extends State<TabScreen> {
  int _selectedIndex = 0;
  final PageController _controller = PageController(initialPage: 0);
  static final List<Widget> _widgetOptions = <Widget>[
    HomeScreen(),
    ShareScreen(),
    MineScreen(),
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
    _controller.jumpToPage(index);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      body: PageView(
        controller: _controller,
        children: _widgetOptions,
        physics: const NeverScrollableScrollPhysics(),
      ),
      bottomNavigationBar: Container(
        padding: EdgeInsets.only(bottom: MediaQuery.of(context).padding.bottom),
        decoration: BoxDecoration(
          color: Colors.white,
        ),
        child: Row(
          children: [
            _buildTabItem(
              icon: Icons.home_outlined,
              activeIcon: Icons.home,
              label: '首页',
              index: 0,
              blackColor: Colors.green,
            ),
            _buildTabItem(
              icon: Icons.explore_outlined,
              activeIcon: Icons.explore,
              label: '分享',
              index: 1,
              blackColor: Colors.pink,
            ),
            _buildTabItem(
              icon: Icons.person_outlined,
              activeIcon: Icons.person,
              label: '我的',
              index: 2,
              blackColor: Colors.blue,
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildTabItem({
    required IconData icon,
    required IconData activeIcon,
    required String label,
    required int index,
    required Color blackColor,
  }) {
    final isSelected = _selectedIndex == index;
    return Expanded(
      child: Material(
        color: Colors.transparent,
        child: InkResponse(
          onTap: () => _onItemTapped(index),
          highlightColor: Colors.transparent,
          radius: 30, // 控制水波纹扩散半径
          containedInkWell: true, // 限制在边界内
          borderRadius: BorderRadius.circular(50),
          child: Container(
            height: kBottomNavigationBarHeight,
            width: 30,
            padding: const EdgeInsets.symmetric(vertical: 6),
            decoration: BoxDecoration(
              shape: BoxShape.circle,
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Stack(
                  alignment: Alignment.center,
                  children: [
                    if (isSelected)
                      Positioned(
                        bottom: 2,
                        right: 2,
                        child: Container(
                          width: 12,
                          height: 12,
                          decoration: BoxDecoration(
                            color: blackColor.withOpacity(0.5),
                            shape: BoxShape.circle,
                          ),
                        ),
                      ),
                    Icon(
                      icon,
                      size: 26,
                      color: const Color.fromARGB(221, 127, 125, 125),
                    ),
                  ],
                ),
                Text(
                  label,
                  style: TextStyle(
                    fontSize: 12,
                    color: isSelected ? Color.fromARGB(221, 127, 125, 125) : Colors.grey,
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
